import {defineStore} from 'pinia'
import {reactive} from 'vue'
import {deleteByIdReq, pageQueryReq} from "@/api/media/material.ts";
import {MaterialImage, MaterialPageQueryReq} from "@/api/media/type.ts";

export const useMediaStore = defineStore('media', () => {
    return {
        materialInfo: reactive({
            // 素材列表
            mediaImageList: [] as MaterialImage[],
            // 素材总数
            total: 0,
            // 当前页码
            pageNum: 1,
            // 每页显示条目个数
            pageSize: 10,
        }),
        clearMaterialInfo() {
            this.materialInfo = reactive({
                mediaImageList: [] as MaterialImage[],
                total: 0,
                pageNum: 1,
                pageSize: 10,
            })
        },
        pageParam :reactive({
            // 当前页码
            pageNum: 2,
            // 每页显示条目个数
            pageSize: 5,
        }),
        /**
         * 分页查询素材列表
         */
        async pageQuery(data: MaterialPageQueryReq) {
            const res = await pageQueryReq(data)
            console.log('pageQuery res', res)
            if (res.code === 200) {
                this.materialInfo.mediaImageList = res.data
                this.materialInfo.mediaImageList.forEach(item => item.showButton = false)
                this.materialInfo.total = res.total
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        },
        /**
         * 根据id删除素材
         */
        async deleteMaterialById(id: number) {
            const res = await deleteByIdReq(id)
            if (res.code === 200) {
                return Promise.resolve(res)
            } else {
                return Promise.reject(res)
            }
        }
    }
}, {
    persist: true,
})
